package com.shop.cereshop.app.service.distributor.impl;

import com.shop.cereshop.app.dao.distributor.CereShopDistributorDAO;
import com.shop.cereshop.app.page.order.ShopDistributor;
import com.shop.cereshop.app.param.distributor.ShopDistributorParam;
import com.shop.cereshop.app.param.extension.ExtensionParam;
import com.shop.cereshop.app.redis.service.api.StringRedisService;
import com.shop.cereshop.app.service.distributor.CereDistributorBuyerService;
import com.shop.cereshop.app.service.distributor.CereShopDistributorService;
import com.shop.cereshop.app.service.redis.CereRedisKeyServcice;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.constant.StringEnum;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.distributor.CereDistributorBuyer;
import com.shop.cereshop.commons.domain.distributor.CereShopDistributor;
import com.shop.cereshop.commons.domain.shop.CereShopRelationship;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.RandomStringUtil;
import com.shop.cereshop.commons.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CereShopDistributorServiceImpl implements CereShopDistributorService {
    @Value("${Dl_Message.shopId}")
    private Long shopId;

    @Autowired
    private CereShopDistributorDAO cereShopDistributorDAO;

    @Autowired
    private CereDistributorBuyerService cereDistributorBuyerService;

    @Autowired
    private StringRedisService stringRedisService;

    @Autowired
    private CereRedisKeyServcice cereRedisKeyServcice;

    @Override
    public ShopDistributor findByPhone(String phone, Long shopId) {
        return cereShopDistributorDAO.findByPhone(phone, shopId);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void addDistributor(ShopDistributorParam param) throws CoBusinessException {
        String time = TimeUtils.yyMMddHHmmss();
        //查询最低等级分销员等级
        Long levelId = cereShopDistributorDAO.findMinLevel();
        if (!EmptyUtils.isEmpty(levelId)) {
            //新增分销员数据
            CereShopDistributor cereShopDistributor = new CereShopDistributor();
            cereShopDistributor.setDistributorName(param.getDistributorName());
            cereShopDistributor.setDistributorPhone(param.getDistributorPhone());
            cereShopDistributor.setDistributorLevelId(levelId);
            cereShopDistributor.setCreateTime(time);
            //根据邀请码查询上级分销员id
            CereShopDistributor distributor = cereShopDistributorDAO.findInvitees(param.getInvitationCode());
            if (distributor != null) {
                //查询该分销员所属店铺关系设置数据
                CereShopRelationship relationship = cereShopDistributorDAO.findRelation(distributor.getShopId());
                if (relationship != null) {
                    //校验是否允许邀请下级
                    if (IntegerEnum.YES.getCode().equals(relationship.getIfInvitation())) {
                        //允许
                        cereShopDistributor.setInvitees(distributor.getDistributorId());
                    }
                }
            }
            cereShopDistributor.setShopId(param.getShopId());
            cereShopDistributor.setState(IntegerEnum.DISTRIBUTOR_STAY.getCode());
            //生成10位数随机邀请码
            cereShopDistributor.setInvitationCode(RandomStringUtil.getRandomCode(10, 0));
            //默认未清退
            cereShopDistributor.setIfLiquidation(IntegerEnum.NO.getCode());
//            cereShopDistributor.setBuyerUserId(cereBuyerUser.getBuyerUserId());
            cereShopDistributorDAO.insert(cereShopDistributor);
        } else {
            throw new CoBusinessException(CoReturnFormat.NOT_HAVE_DISTRIBUTOR_LEVEL);
        }
    }

    @Override
    public String findInvitationCode(Long distributorId) {
        return cereShopDistributorDAO.findInvitationCode(distributorId);
    }

    @Override
    public CereShopDistributor check(Long shopId, Long buyerUserId) {
        return cereShopDistributorDAO.check(shopId, buyerUserId);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void bind(ExtensionParam param, CereBuyerUser user) throws CoBusinessException, Exception {
        //定义是否新增绑定关系(默认不新增)
        boolean flag = true;
        String time = TimeUtils.yyMMddHHmmss();
        //查询当前分销员是否已绑定过该用户
        CereDistributorBuyer cereDistributorBuyer = cereDistributorBuyerService.findByDisAndUser(param.getDistributorId(), user.getBuyerUserId());
        //查询当前客户是否已绑定其他分销员
        CereDistributorBuyer distributorBuyer = cereDistributorBuyerService.checkUser(param.getDistributorId(), user.getBuyerUserId());
        if (distributorBuyer != null) {
            //如果绑定了,查询该分销员所属店铺关系设置数据
            CereShopRelationship relationship = cereShopDistributorDAO.findRelation(distributorBuyer.getShopId());
            if (relationship != null) {
                if (IntegerEnum.CAN_GRAB_USER.getCode().equals(relationship.getIfRobbing())) {
                    //随时可抢
                } else if (IntegerEnum.NOT_GRAB_USER.getCode().equals(relationship.getIfRobbing())) {
                    //不允许抢客
                    flag = false;
                } else {
                    //保护期几天内不允许抢
                    if (!EmptyUtils.isEmpty(relationship.getRobbingDay())) {
                        //计算关系绑定时间到当前时间有效天数
                        int day = TimeUtils.differentDaysByMillisecond(distributorBuyer.getUpdateTime(), time);
                        if (day <= relationship.getRobbingDay()) {
                            //如果时间没有超出了保护期天数,不允许抢客
                            flag = false;
                        }
                    }
                }
            }
        }
        if (flag) {
            if (cereDistributorBuyer != null) {
                //更新绑定状态
                cereDistributorBuyer.setIfBind(IntegerEnum.YES.getCode());
                cereDistributorBuyer.setUpdateTime(time);
                cereDistributorBuyerService.update(cereDistributorBuyer);
            } else {
                //新增绑定数据
                cereDistributorBuyer = new CereDistributorBuyer();
                cereDistributorBuyer.setDistributorId(param.getDistributorId());
                cereDistributorBuyer.setBuyerUserId(user.getBuyerUserId());
                cereDistributorBuyer.setShopId(shopId);
                cereDistributorBuyer.setIfBind(IntegerEnum.YES.getCode());
                cereDistributorBuyer.setUpdateTime(time);
                cereDistributorBuyerService.insert(cereDistributorBuyer);
            }
            //根据店铺id查询关系设置数据
            CereShopRelationship relationship = cereShopDistributorDAO.findRelation(param.getShopId());
            if (relationship != null) {
                if (!EmptyUtils.isEmpty(relationship.getValidityDay())) {
                    //如果设置了有效天数,在有效天数达到之后解除绑定关系
                    stringRedisService.set(StringEnum.SHOP_VALIDITY_DAT.getCode() + "-" + cereDistributorBuyer.getDistributorId() +
                                    "-" + cereDistributorBuyer.getBuyerUserId(), 1,
                            relationship.getValidityDay() * 24 * 60 * 60 * 1000);
                    //更新redis延时任务记录
                    cereRedisKeyServcice.updateByKey(StringEnum.SHOP_VALIDITY_DAT.getCode() + "-" + cereDistributorBuyer.getDistributorId() +
                                    "-" + cereDistributorBuyer.getBuyerUserId(),
                            TimeUtils.getMoreDayAfter(TimeUtils.yyMMddHHmmss(), relationship.getValidityDay()));
                }
            }
        }
    }

    @Override
    public ShopDistributor findById(Long sharerId) {
        return cereShopDistributorDAO.findById(sharerId);
    }
}
